package util

import (
	"bytes"
	"encoding/json"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
)

func GetTargetURL(u string) *url.URL {
	if u == "" {
		log.Fatal("url is empty")
		return nil
	}
	targetUrl, error := url.Parse(u)
	if error != nil {
		log.Fatal("err")
		return nil
	}
	return targetUrl
}

func ParseResponse(r *http.Response, unmarshalStruct interface{}) error {
	body, error := ioutil.ReadAll(r.Body)
	if error != nil {
		return error
	}
	r.Body.Close()
	r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
	return json.Unmarshal(body, unmarshalStruct)
}
